﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace RelationTextBox
{
    /// <summary>
    /// Summary description for DateTimePicker.
    /// </summary>
    public partial class DateTimePicker : System.Windows.Forms.DateTimePicker
    {
        //private DateTimePickerFormat oldFormat = DateTimePickerFormat.Long;
        //private string oldCustomFormat = null;
        private bool bIsNull = false;
        public DateTime NullDate = new DateTime(1753, 1, 1);

        public DateTimePicker()
            : base()
        {
        }

        public new DateTime Value
        {
            get
            {
                if (!Checked)
                    return NullDate;
                else
                    return base.Value;
            }
            set
            {
                if (value == NullDate)
                {
                    Checked = false;
                    ShowCheckBox = true;
                    
                    /*if (Checked == false)
                    {
                        //oldFormat = this.Format;
                        //oldCustomFormat = this.CustomFormat;
                        Checked = true;
                        ShowCheckBox = true;
                        //Checked = false;
                    }

                    //this.Format = DateTimePickerFormat.Custom;
                    //this.CustomFormat = " ";
                     * */
                }
                else
                {
                    Checked = true;
                    ShowCheckBox = true;

                    /*if (Checked)
                    {
                        //this.Format = oldFormat;
                        //this.CustomFormat = oldCustomFormat;
                        Checked = false;
                        ShowCheckBox = true;
                        //Checked = true;
                    }*/
                    if (value <= base.MaxDate && value >= base.MinDate)
                        base.Value = value;
                    else
                        base.Value = base.MaxDate;
                }
            }
        }

        protected override void OnCloseUp(EventArgs eventargs)
        {
            if (Control.MouseButtons == MouseButtons.None)
            {
                if (bIsNull)
                {
                    //this.Format = oldFormat;
                    //this.CustomFormat = oldCustomFormat;
                    bIsNull = false;
                }
            }
            base.OnCloseUp(eventargs);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            if (e.KeyCode == Keys.Delete)
                this.Value = NullDate;
        }
    }
}
